﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Data;
using DAL;
using BLL;
using System.Text;
using Telerik.Web.UI;

namespace WebUI.Pages
{
    public partial class mgrSolicitacoes : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UserController userController = new UserController();

            int userID = userController.GetUserID(User.Identity.Name);
            int teamID = userController.GetUserTeamID(userID);

        }
        protected void gridSolicitacoes_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            UserController usrController = new UserController();
            ConnectionManager connMgr = new ConnectionManager();

            StringBuilder qryX = new StringBuilder();
            qryX.Append(@"SELECT   [idSolicitacao] ,
                                   U2.Nome as AnalistaNegocios ,
                                   U.Nome as AccountManager
                                  ,Titulo AS ShortDesc
                                  ,[DataSolicitacao]
                                  ,TpS.DescTipoSolicitacao as TipoSolicitacao
                                  ,[DataExpectativa]
                                  ,[Cliente]
                                  ,F.DescFabricante
                                  ,EV.DescEstagioVenda as EstagioVendas
                                  ,[PeriodoExpectativa]
                                  ,SLA.SLA as DescSLA
                                  ,U3.Nome as Criador
                                  ,[DataDesignacao]
                                  ,[DataEntrega]
                                  ,[DataFinalizacao]
                                  ,StatusList.DescStatus as DescSL
                                   FROM Solicitacoes as S
                                   LEFT OUTER JOIN Usuarios as U ON S.idAccountManager = U.idUsuario
                                   LEFT OUTER JOIN Usuarios as U2 ON S.idAnalistaNegocios = U2.idUsuario
                                   LEFT OUTER JOIN Usuarios as U3 ON S.idCriador = U3.idUsuario
                                   INNER JOIN Fabricantes as F ON S.idFabricante = F.idFabricante
                                   INNER JOIN TiposSolicitacao as TpS ON S.idTipoSolicitacao = TpS.idTipoSolicitacao
                                   INNER JOIN EstagiosVenda as EV ON S.idEstagioVendas = EV.idEstagioVenda
                                   INNER JOIN SLA ON S.idSLA = SLA.idSLA
                                   INNER JOIN StatusList ON [S].[Status] = StatusList.[Status]
                                   WHERE SLA LIKE '%'  ");
            
            qryX.Append(" ORDER BY DataSolicitacao DESC");
            SqlCommand qry = new SqlCommand(qryX.ToString());
            gridSolicitacoes.DataSource = connMgr.doReader(qry);
        }

        protected void cmdSearch_Click(object sender, EventArgs e)
        {
            UserController usrController = new UserController();
            ConnectionManager connMgr = new ConnectionManager();

            StringBuilder qryX = new StringBuilder();
            qryX.Append(@"SELECT   [idSolicitacao] ,
                                   U2.Nome as AnalistaNegocios ,
                                   U.Nome as AccountManager
                                  ,Titulo AS ShortDesc
                                  ,[DataSolicitacao]
                                  ,TpS.DescTipoSolicitacao as TipoSolicitacao
                                  ,[DataExpectativa]
                                  ,[Cliente]
                                  ,F.DescFabricante
                                  ,EV.DescEstagioVenda as EstagioVendas
                                  ,[PeriodoExpectativa]
                                  ,SLA.SLA as DescSLA
                                  ,U3.Nome as Criador
                                  ,[DataDesignacao]
                                  ,[DataEntrega]
                                  ,[DataFinalizacao]
                                  ,StatusList.DescStatus as DescSL
                                   FROM Solicitacoes as S
                                   LEFT OUTER JOIN Usuarios as U ON S.idAccountManager = U.idUsuario
                                   LEFT OUTER JOIN Usuarios as U2 ON S.idAnalistaNegocios = U2.idUsuario
                                   LEFT OUTER JOIN Usuarios as U3 ON S.idCriador = U3.idUsuario
                                   INNER JOIN Fabricantes as F ON S.idFabricante = F.idFabricante
                                   INNER JOIN TiposSolicitacao as TpS ON S.idTipoSolicitacao = TpS.idTipoSolicitacao
                                   INNER JOIN EstagiosVenda as EV ON S.idEstagioVendas = EV.idEstagioVenda
                                   INNER JOIN SLA ON S.idSLA = SLA.idSLA
                                   INNER JOIN StatusList ON [S].[Status] = StatusList.[Status]
                                   WHERE SLA LIKE '%'  ");

            qryX.Append(" ORDER BY DataSolicitacao DESC");
            SqlCommand qry = new SqlCommand(qryX.ToString());
            gridSolicitacoes.DataSource = connMgr.doReader(qry);
            gridSolicitacoes.DataBind();
        }

        protected void rpSolicitacoes_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "cmdEdit")
            {
                Response.Redirect("../Pages/edit.aspx?id=" + e.CommandArgument.ToString());
            }
        }


    }
}
